home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / cexpress / strings / padends.asm < prev    next >
Encoding:
Assembly Source File  |  1989-05-03  |  2.1 KB  |  78 lines

  1. ;void  pad_ends(strg,ch,length);
  2. ;  unsigned char  *strg,ch;
  3. ;  unsigned short  length;
  4.  
  5.     EXTRN  _memory_model:byte
  6.     EXTRN  _error_code:byte
  7.  
  8. _TEXT    SEGMENT BYTE PUBLIC 'CODE'
  9.     ASSUME CS:_TEXT
  10.     PUBLIC _pad_ends
  11. _pad_ends proc near
  12.     push bp            ;
  13.     mov  bp,sp        ;set up stack frame
  14.     push di            ;
  15.     push si            ;
  16.     push ds            ;save DS
  17.     mov  _error_code,1    ;1 = error
  18.     cmp  _memory_model,0    ;near or far?
  19.     jle  begin        ;jump if near
  20.     inc  bp            ;else add 2 to BP
  21.     inc  bp            ;
  22. begin:    cmp  _memory_model,2    ;data near or far?
  23.     jb   L0            ;jump if near
  24.     les  di,dword ptr[bp+4] ;get string address
  25.     mov  ax,es        ;DS = ES
  26.     mov  ds,ax        ;
  27.     inc  bp            ;add 2 to bp since dword ptr
  28.     inc  bp            ;
  29.     jmp  short L1        ;jump ahead
  30. L0:    mov  di,[bp+4]        ;near case
  31.     mov  ax,ds        ;
  32.     mov  es,ax        ;
  33. L1:    sub  cx,cx        ;must find string length
  34.     mov  si,di        ;copy string ptr
  35. L2:    cmp  byte ptr[si],0     ;end of string?
  36.     je   L3            ;
  37.     inc  si            ;
  38.     inc  cx            ;
  39.     jmp  short L2        ;loop till null
  40. L3:    sub  bx,bx        ;
  41.     mov  bl,[bp+8]        ;new string length in BL
  42.     cmp  cl,bl        ;compare the two lengths
  43.     jae  L5            ;quit if old len >= new
  44.     sub  bx,cx        ;number of spaces to pad
  45.     mov  dx,bx        ;copy
  46.     shr  dx,1        ;number to pad on right
  47.     sub  bx,dx        ;number to pad on left
  48.     mov  di,si        ;pt DI to new string end
  49.     add  di,bx        ;
  50.     inc  cx            ;move extra char for null
  51.     push di            ;save end-of-string position
  52.     std            ;set direction flag
  53.     rep  movsb        ;move old string right
  54.     mov  cx,bx        ;spaces to pad in CX
  55.     mov  al,[bp+6]        ;get pad character
  56. L4:    mov  es:[di],al        ;insert pad char
  57.     dec  di            ;move to next position
  58.     loop L4            ;go do next char
  59.     mov  cx,dx        ;counter for right pad chars
  60.     pop  di            ;get end-of-string position
  61.     cld            ;direction forward
  62.     rep  stosb        ;write right pad chars
  63.     mov  byte ptr es:[di],0 ;terminator
  64.     pop  ds            ;restore DS
  65.     dec  _error_code    ;0 = no error
  66.     jmp  short L6        ;jump ahead and quit
  67. L5:    pop  ds            ;exit with error
  68. L6:    pop  si            ;
  69.     pop  di            ;
  70.     pop  bp            ;
  71.     cmp  _memory_model,0    ;quit
  72.     jle  quit        ;
  73.     db   0CBh        ;RET far
  74. quit:    ret            ;RET near
  75. _pad_ends ENDP
  76. _TEXT    ENDS
  77.     END
  78.